/*
 * xAPBSCendpoint.java
 *
 * Java class used to read XML config files
 * ATTENTION: All xAP config files must be stored in the
 * Elements/xAP folder
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 07/11/09 by Daniel Berenguer : first version
 */

package xAP;

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


/********************************************************************************************
 * configParser class
 *
 *********************************************************************************************/
public class configParser
{
   /********************************************************************************************
    * readGeneral
    *
    * Read general config file
    *
    * 'fileName'       Name of the config file
    *
    ********************************************************************************************/
    public static void readGeneral(String fileName)
    {
        try
        {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(fileName));

            // normalize text representation
            doc.getDocumentElement().normalize ();

            // xAP enable flag
            NodeList listEnables = doc.getElementsByTagName("enable");
            if (listEnables.getLength() == 1)
            {
                Element elemEnable = (Element)listEnables.item(0);
                NodeList listEnable = elemEnable.getChildNodes();
                if (((Node)listEnable.item(0)).getNodeValue().trim().equalsIgnoreCase("true"))
                    drvCore.xAP_Run = true;
                else
                    drvCore.xAP_Run = false;
            }

            // xAP instance name
            NodeList listNames = doc.getElementsByTagName("name");
            if (listNames.getLength() == 1)
            {
                Element elemName = (Element)listNames.item(0);
                NodeList listName = elemName.getChildNodes();
                drvCore.xAP_SourceAddr = drvCore.xAP_AddressBase + "." + ((Node)listName.item(0)).getNodeValue().trim();
            }

            // xAP UID base address
            NodeList listUIDs = doc.getElementsByTagName("uidbase");
            if (listUIDs.getLength() == 1)
            {
                Element elemUID = (Element)listUIDs.item(0);
                NodeList listUID = elemUID.getChildNodes();
                drvCore.xAP_BaseUID = ((Node)listUID.item(0)).getNodeValue().trim();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
